﻿/*************************************************************************************
 * 文 件 名:   HK_FirstPersonCtrl
 * 
 * 描    述:   HK_FirstPersonCam 角色总控脚本
 * 
 * 版    本：  V1.0
 * 创 建 者：  京产肠饭
 * 创建时间：  2023/6/27
 * 链   接：  https://gitee.com/JingChanChangFan/hk_-unity-tools
*************************************************************************************/

using UnityEngine;
using QFramework;
using DG.Tweening;
using UnityEngine.EventSystems;

namespace HKTools
{
    public class HK_FirstPersonCtrl : HK_FirstPersonCamController
    {
        [Header("开启UI遮挡："), SerializeField] bool isUIBlock;

        [Header("移动速度："), SerializeField] float moveSpeed = 6f;
        [Header("重力加速度："), SerializeField] float gravity = 9.81f;
        [Header("跳跃高度："), SerializeField] float jumpHeight = 1f;

        [Header("地面检测物体："), SerializeField] Transform groundCheck;
        [Header("地面检测半径："), SerializeField] float groundDistance = 0.3f;
        [Header("地面检测层级："), SerializeField] LayerMask groundMask;

        [Header("相机的Mask"), SerializeField] GameObject mask_Go;

        CharacterController controller;

        HK_FirstPersonCamLook firstPersonCamLook;

        Vector3 velocity;

        [SerializeField] bool isGrounded;

        [SerializeField] bool isSpeedUp;

        void Awake()
        {
            controller = GetComponent<CharacterController>();
            firstPersonCamLook = GetComponentInChildren<HK_FirstPersonCamLook>();
            mask_Go.SetActive(false);

            firstPersonCamLook.isUIBlock = isUIBlock;
        }

        void Start()
        {
            this.SendCommand(new CMD_InitFirstPersonCam(this));
        }

        void Update()
        {
            if (EventSystem.current.IsPointerOverGameObject() && isUIBlock)
                return;

            if (controller == null || !controller.enabled)
                return;

            isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);

            if (isGrounded && velocity.y < 0)
            {
                velocity.y = -2f;
            }

            if (Input.GetKey(KeyCode.LeftShift))
            {
                if (!isSpeedUp)
                    isSpeedUp = true;
            }
            else
            {
                if (isSpeedUp)
                    isSpeedUp = false;
            }

            float x = Input.GetAxis("Horizontal");
            float z = Input.GetAxis("Vertical");

            Vector3 motion = transform.right * x + transform.forward * z;

            if (isSpeedUp)
                controller.Move(motion * moveSpeed * 3 * Time.deltaTime);
            else
                controller.Move(motion * moveSpeed * Time.deltaTime);

            if (Input.GetButtonDown("Jump") && isGrounded)
            {
                velocity.y = Mathf.Sqrt(jumpHeight * 2f * gravity);
            }

            velocity.y -= gravity * Time.deltaTime;

            controller.Move(velocity * Time.deltaTime);
        }

        public void ChangeRot(Transform pos_TF)
        {
            transform.rotation = Quaternion.Euler(0, pos_TF.eulerAngles.y, 0);
            firstPersonCamLook.ResetRot();
        }

        public void ShowMask()
        {
            mask_Go.SetActive(true);
            mask_Go.GetComponent<Renderer>().material.DOFade(1, 0.4f);
            mask_Go.GetComponent<Renderer>().material.DOFade(0, 0.4f).SetDelay(0.4f).OnComplete(() => mask_Go.SetActive(false));
        }

        public void ResetRot()
        {
            firstPersonCamLook.ResetRot();
        }
    }
}